package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingProjectPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingPlanProjectMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingPlanProjectMapper nursingPlanProjectMapper;

    /**
     * 条件分页查询护理计划
     *
     * @param pageNum
     * @param pageSize
     * @param status
     * @param planName
     * @return
     */
    @Override
    public PageResponse<NursingPlan> selectByPage(Integer pageNum, Integer pageSize, Integer status, String planName) {
        PageHelper.startPage(pageNum, pageSize);
        Page<List<NursingPlan>> lists = nursingPlanMapper.selectByPage(pageNum, pageSize, status, planName);// 获取所有护理计划
        return PageResponse.of(lists, NursingPlan.class);
    }

    /**
     * 查询所有护理计划
     *
     * @return
     */
    @Override
    public List<NursingPlanVo> finAll() {
        List<NursingPlanVo> vo = nursingPlanMapper.findAll();
        return vo;
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlanDto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertPlan(NursingPlanDto nursingPlanDto) {
        //新增护理计划的时候需要对护理计划名称做出重复判断
        NursingPlanVo vo = nursingPlanMapper.selectByName(nursingPlanDto.getPlanName());
        if (ObjectUtil.isNotEmpty(vo)) {
            throw new BaseException(BasicEnum.NURSE_PLAN_REPEAT_FAIL);
        }


        NursingPlan nursingPlan = BeanUtil.copyProperties(nursingPlanDto, NursingPlan.class);
        nursingPlan.setStatus(0);
        nursingPlanMapper.insertPlan(nursingPlan);


        // 插入 projectPlans
        List<NursingProjectPlanDto> projectPlans = nursingPlan.getProjectPlans();

        if (projectPlans != null && !projectPlans.isEmpty()) {
            for (NursingProjectPlanDto projectPlan : projectPlans) {
                projectPlan.setPlanId(nursingPlan.getId());
            }
            nursingPlanMapper.insertProjectPlan(projectPlans);
        }


    }

    /**
     * 根据id查询护理计划
     *
     * @param id
     * @return
     */
    @Override
    public NursingPlanVo selectById(Long id) {

        NursingPlanVo vo = nursingPlanMapper.selectById(id);
        List<NursingProjectPlanVo> list = nursingPlanProjectMapper.selectByPlanId(id);
        vo.setProjectPlans(list);
        return vo;
    }

    @Override
    public void updateById(Long id, NursingPlanDto nursingPlanDto) {
        if (id == null || id <= 0) {
            throw new BaseException(BasicEnum.INVALID_ID);
        }
        //1,先修改护理计划表的数据
        nursingPlanMapper.editNursingPlan(nursingPlanDto);

        //2,再修改护理计划和项目的关联表

        nursingPlanMapper.updateProjectPlans(nursingPlanDto.getProjectPlans()); // 假设有一个方法来更新项目计划


    }

    @Override
    public void deleteById(Long id) {
        nursingPlanMapper.deleteById(id);
    }

    @Override
    public void openPlanStatus(Long id, Long status) {
        nursingPlanMapper.openPlanStatus(id, status);
    }
}
